package com.woniuxy.service.impl;

import com.woniuxy.entity.*;
import com.woniuxy.mapper.*;
import com.woniuxy.service.FoodService;
import com.woniuxy.vo.FoodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/*
 * 美食实现类
 */
@Service
public class FoodServiceImp  implements FoodService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private FoodMapper foodMapper;
    @Resource
    private FoodCommentMapper foodCommentMapper;
    @Resource
    private FoodOrderMapper foodOrderMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private FoodCollectionMapper foodCollectionMapper;
    @Resource
    private UserMapper userMapper;

    /**
     *  按照美食名  和营地地点动态条件查询 美食
     * @param map
     * @return
     */
    @Override
    public List<Food> selectByFoodNameAndCampId(Map map) throws Exception {
        List<Food> foods = foodMapper.selectByFoodNameAndCampId(map);
        return foods;
    }

    /**
     * 查询美食商品详情需要foodId
     * @param foodId
     * @return
     */
    @Override
    public Food selectFoodByFoodId(Integer foodId) throws Exception {
        Food foods = foodMapper.selectFoodByFoodId(foodId);
        return foods;
    }

    /**
     * 查询该foodId的所有评论
     * @param map
     * @return
     */
    @Override
    public List<FoodComment> selectAllFoodCommentByFoodId(Map map) throws Exception {
        List<FoodComment> foodComments = foodCommentMapper.selectFoodCommentByFoodId(map);
        return foodComments;
    }

    /**
     * 按照用户的id查询美食订单    当美食状态是刚下订单的15分钟内时进行剩余时间的赋值
     * @param map
     * @return
     */
    @Override
    public List<FoodVO> selectAllFoodOrderByUserId(Map<String,Object> map) throws Exception {
        List<FoodVO> foodOrders = foodOrderMapper.selectAllFoodOrderByUserId(map);
        for (int i = 0; i <foodOrders.size() ; i++) {
            Integer status = foodOrders.get(i).getStatus();
            if(status == 2){
                Long seconds = redisTemplate.opsForValue().getOperations().getExpire("foodOrder#"+ foodOrders.get(i).getFoodOrderId());//此方法返回单位为秒过期时长
                int ss = (int)(seconds % 60);
                int mm = (int) (seconds / 60);
                String  time="mm"+":"+"ss";
                SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss");
                Date date=null;
                date=formatter.parse(time);
                foodOrders.get(i).setRemainingTime(date);
            }
        }
        return foodOrders;
    }

    /**
     * 根据营地id查询营地正在使用的车
     * @param campId
     * @return
     */
    @Override
    public List<Car> selectAllCarByCampId(Integer campId) throws Exception {
        CarExample carExample = new CarExample();
        CarExample.Criteria criteria = carExample.createCriteria();
        criteria.andCampIdEqualTo(campId);
        criteria.andStatusEqualTo(1);
        List<Car> cars = carMapper.selectByExample(carExample);
        return cars;
    }

    /**
     * 新增美食的订单
     * @param foodOrder
     * @return
     */
    @Override
    public int insertFoodOrder(FoodOrder foodOrder) {
        int i = foodOrderMapper.insertSelective(foodOrder);
        //得到新增的主键
        String key = "foodOrder#"+foodOrder.getFoodOrderId();
        ValueOperations forValue = redisTemplate.opsForValue();
        forValue.set(key,"123", 900, TimeUnit.SECONDS);
        return i;
    }

    /**
     *查询该商品是否收藏
     * @param foodId
     * @param userId
     * @return
     */
    @Override
    public FoodCollection judgeUserFoodCollection(int foodId, int userId) {
        FoodCollectionExample example = new FoodCollectionExample();
        example.createCriteria().andFoodIdEqualTo(foodId).andUserIdEqualTo(userId);
        List<FoodCollection> foodCollections = foodCollectionMapper.selectByExample(example);
        if (foodCollections.size()!=0){
            return foodCollections.get(0);
        }else{
            return null;
        }
    }

    /**
     * 添加收藏
     * @param foodCollection
     * @return
     */
    @Override
    public int insertFoodCollection(FoodCollection foodCollection) {
        return foodCollectionMapper.insert(foodCollection);
    }

    /**
     * 取消收藏
     * @param foodId
     * @param userId
     * @return
     */
    @Override
    public int deleteCollection(int foodId, int userId) {
        FoodCollectionExample example = new FoodCollectionExample();
        example.createCriteria().andUserIdEqualTo(userId).andFoodIdEqualTo(foodId);
        return foodCollectionMapper.deleteByExample(example);
    }

    /**
     * 查询该用户美食所有收藏
     * @param userId
     * @return
     */
    @Override
    public List<FoodCollection> selectAllCollection(int userId) {
        return foodCollectionMapper.selectAllCollection(userId);
    }

    /**
     * 根据id删除收藏
     * @param id
     * @return
     */
    @Override
    public int deleteCollectionById(int id) {
        return foodCollectionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 用户支付完成之后的评论美食或者从订单中查询送达的美食进行评论
     * @param foodId
     * @param userId
     * @param comment
     * @return
     */
    @Override
    public int insertFoodComment(Integer foodId, Integer userId, String comment) {
        FoodComment foodComment = new FoodComment();
        java.util.Date date = new java.util.Date(); // 获取一个Date对象
        Timestamp timeStamp = new Timestamp(date.getTime()); // 将日期时间转换为数据库中的timestamp类型
        foodComment.setTime(timeStamp);
        foodComment.setComment(comment);
        foodComment.setFoodId(foodId);
        foodComment.setUserId(userId);
        int i = foodCommentMapper.insertSelective(foodComment);
        return i;
    }

    /**
     * 用户支付时候 的密码验证
     * @param userId
     * @return
     */
    @Override
    public User selectUserInfoByuserId(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    /**
     * 用户取消美食订单的
     * @param foodOrderId
     * @return
     */
    @Override
    public int deleteFoodOrder(Integer foodOrderId) {
        int i = foodOrderMapper.deleteByPrimaryKey(foodOrderId);
        return i;
    }

    /**
     * 查询用户是否对美食评论过
     * @param foodId
     * @param userId
     * @return
     */
    @Override
    public int selectIsOrYesFoodComment(Integer foodOrderId,Integer foodId, Integer userId) {
        int res = foodCommentMapper.selectIsOrYesFoodComment(foodOrderId, foodId, userId);
        return res;
    }

    @Override
    public void updateOrderStatusById(Integer id) {
        FoodOrder order = new FoodOrder();
        order.setStatus(5);
        order.setFoodOrderId(id);
        foodOrderMapper.updateByPrimaryKeySelective(order);
    }


}
